package sanrenxing.dayEnd.backRepertoryTotal.dao;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import sanrenxing.dayEnd.backRepertoryTotal.domain.*;

import java.util.List;

/**
 * @desrciption 证券交易数据dao层
 * @auth：yidu 李小锋
 * @date:2021/3/12 0012 - 9:09
 * @version:1:0
 */
@Repository //标识为Spring中的Bean
public interface TransactionDao {
    /**
     * 新增一行数据
     * @param transaction 对象
     * @return 0、增加失败 1、增加成功
     */
    @Insert("insert into securitiesTradingData" +
            "(tranId,tranDate,tranNum,price,settMoney,tranMoney,settDate,fundId," +
            "seId,stockId,seatId,caId,may,BreedRateId,seInte,tranStatus,actMoney,mark)" +
            "values(#{tranId},#{tranDate},#{tranNum},#{price},#{settMoney},#{tranMoney},#{settDate},#{fundId}," +
            "#{seId},#{stockId},#{seatId},#{caId},#{may},#{BreedRateId},#{seInte},#{tranStatus},#{actMoney},#{mark})")
    public int add(Transaction transaction);

    /**
     * excl新增一行数据
     * @param transactionExcl 对象
     * @return 0、增加失败 1、增加成功
     */
    @Insert("insert into securitiesTradingData" +
            "(tranId,tranDate,tranNum,price,settMoney,tranMoney,settDate,fundId," +
            "seId,stockId,seatId,caId,may,BreedRateId,tranStatus,actMoney,mark)" +
            "values(#{tranId},to_date('${tranDate}','yyyy-MM-dd'),#{tranNum},#{price},#{settMoney},#{tranMoney},to_date('${settDate}','yyyy-MM-dd'),#{fundId}," +
            "#{seId},#{stockId},#{seatId},#{caId},#{may},#{BreedRateId},#{tranStatus},#{actMoney},#{mark})")
    public int addExcl(TransactionExcl transactionExcl);

    /**
     * 删除一行数据
     * @param tranId 编号
     * @return 0、删除失败 1、删除成功
     */
    @Delete("delete from securitiesTradingData where tranId=#{tranId}")
    public int delete(@Param("tranId") String tranId);

    /**
     * 修改一行数据
     * @param transaction 对象
     * @return 0、修改失败 1、修改成功
     */
    @Update("update securitiesTradingData set tranDate=#{tranDate},tranNum=#{tranNum},price=#{price},settMoney=#{settMoney},tranMoney=#{tranMoney},settDate=#{settDate},fundId=#{fundId}," +
            "seId=#{seId},stockId=#{stockId},seatId=#{seatId},caId=#{caId},may=#{may},BreedRateId=#{BreedRateId},seInte=#{seInte},tranStatus=#{tranStatus},actMoney=#{actMoney},mark=#{mark} where tranId=#{tranId}")
    public int update(Transaction transaction);
    /**
     * 查询所有的订单数据
     * @return 返回一个订单集合
     */
    @Select("select * from  securitiesTradingData")
    @Results(id = "TransMap",value = {
            @Result(id = true,property = "tranId",column = "tranId"),
            @Result(property = "tranDate",column = "tranDate"),
            @Result(property = "tranNum",column = "tranNum"),
            @Result(property = "price",column = "price"),
            @Result(property = "settMoney",column = "settMoney"),
            @Result(property = "tranMoney",column = "tranMoney"),
            @Result(property = "settDate",column = "settDate"),
            @Result(property = "fundId",column = "fundId"),
            @Result(property = "seId",column = "seId"),
            @Result(property = "stockId",column = "stockId"),
            @Result(property = "seatId",column = "seatId"),
            @Result(property = "caId",column = "caId"),
            @Result(property = "may",column = "may"),
            @Result(property = "BreedRateId",column = "BreedRateId"),
            @Result(property = "seInte",column = "seInte"),
            @Result(property = "tranStatus",column = "tranStatus"),
            @Result(property = "actMoney",column = "actMoney"),
            @Result(property = "mark",column = "mark")
    })
    public List<Transaction> findAll();
    /**
     * 分页查询
     * @param start  开始行号
     * @param rows   每页行数
     * @param condition 查询条件
     * @return 集合
     */
    @ResultMap(value = "TransMap")
    @Select("<script>" +
            "select ss.tranId,ss.tranDate,ss.tranNum,ss.price,ss.settMoney,ss.tranMoney,ss.settDate,ss.fundId,ss.seId,ss.stockId,ss.seatId,\n" +
            "ss.caId,ss.may,ss.seInte,ss.tranStatus,ss.actMoney,ss.mark,ee.BreedRateId from " +
            "(select rownum rn,securitiesTradingData.* from securitiesTradingData " +
            "<where> #{rows}>rownum </where>) ss join financialsecurities ff on ss.seId=ff.seId join " +
            "exchangeVarietyRate ee on ff.BreedRateId=ee.BreedRateId" +
            "<where>" +
            " ${condition}" +
            " and rn>=#{start}" +
            "</where>" +
            "</script>"
    )
    public List<Transaction> findByPage(@Param("start") int start, @Param("rows") int rows, @Param("condition") String condition);

    /**
     * 查询券商
     * @return
     */
    @Results(id = "settingMap",value = {
            @Result(property = "stockId",column = "stockId"),
            @Result(property = "stockName",column = "stockName")
    })
    @Select("select stockId,stockName from settingOfSecurities")
    public List<SettingOfSecuritiesLXF>  findSettingOf();

    /**
     * 基金设置
     * @return
     */
    @Results(id = "fund",value = {
            @Result(property = "fundId",column = "fundId"),
            @Result(property = "fundName",column = "fundName")
    })
    @Select("select fundId,fundName from fundSetup")
    public List<FundSetupLXF>  findFund();
    /**
     * 品种设置
     * @return
     */
    @Results(id = "exchange",value = {
            @Result(property = "BreedRateId",column = "BreedRateId"),
    })
    @Select("select *  from exchangeVarietyRate")
    public List<ExchangeVarietyRate>  ExchangeVarietyRateLXF();
    /**
     * 席位设置
     * @return
     */
    @Results(id = "Trading",value = {
            @Result(property = "seatId",column = "seatId"),
            @Result(property = "seatName",column = "seatName")
    })
    @Select("select seatId,seatName from tradingSeat")
    public List<TradingSeatLXF>  TradingSeatLXF();
    /**
     * 证券设置
     * @return
     */
    @Select("select * from financialsecurities")
    public List<Financialsecurities>  SecuritiesInfomationLXF();

    /**
     * 按条件统计出记录行数
     * @param condition 条件
     * @return 记录行数
     */
    @Select("select count(*) from securitiesTradingData where 1=1 ${condition}")
    public int count(@Param("condition") String condition);

    /**
     * 现金账户
     * @return
     */
    @Select("select * from cashAccount")
    public List<CashAccount> queryCash();

    @Select("select * from financialsecurities ff join exchangeVarietyRate ee on ff.BreedRateId=ee.BreedRateId where seId=#{seId}")
    public ExchangeVarietyRate queryEx(String seId);
}
